package com.ebeitech.library.ui;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebeitech.library.R;
import com.ebeitech.library.domain.CommonItem;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;

import java.util.List;

/**
 * Created by alice on 2017/10/31.
 */
public class CommonItemView extends LinearLayout {

	View mHeaderView;
	View mFooterView;
	TextView mTitleText;
	TextView mValueText;
	EditText mValueEdit;
	LinearLayout mMediaLinearLayout;
	android.widget.HorizontalScrollView mMediaParentView;
	NumberVariateView mNumberView;
	ImageView mArrowView;
	ImageView mImageView;
	LinearLayout viewRight;
	ViewGroup mContentView;
	ImageView mDividerView;
	TextView mBtnRight;
	TextView mBtnTextRight;

	public enum ItemStyle {
		DEFAULT, BOOL, BUTTON, SELECT, EDIT, SELECT_SINGLE, NUMBER, ITEMS_GRID
	}

	/*public interface MediaDownloadListener {
		void onMediaDownload(List<DataHolder> mediaDataHolders);
	}*/

	private Context mContext;
	private LayoutInflater mInflater;
	private View mRootView;
	//	private View mSelectSingleView;
	private List<RadioButton> mSelectSingleItemViews;
//	private SingleSelectCheckBoxs mSingleSelectCheckBoxs;

	private String mTitle = "";
	private ItemStyle mStyle = ItemStyle.DEFAULT;
	private boolean isDividerVisible = true;
	private boolean isHearderVisible = false;
	private boolean isFooterVisible = false;
	private TextWatcher mTextWatcher;

	private int mItemMargin;

	private CommonItem mDetailItem;
	//	private List<DataHolder> mMediaDataHolders;
//	private MediaDownloadListener mMediaDownloadListener;
	private boolean mIsDeadlineCanSelect = true;

	private OnItemSelectListener mOnItemSelectListener;
	private OnSelectListener mOnSelectListener;

	public interface OnItemSelectListener {
		void onItemSelect(String itemName);
	}

	public interface OnSelectListener {
		void onSelect(boolean isSelect);
	}

	/*private OnClickListener listener = new OnClickListener() {

		private ProgressDialog mProgressDialog;

		@Override
		public btn_inspect_public_selected.pngView v) {

			final Activity activity = (Activity) mContext;
			final DataHolder holder = (DataHolder) v.getTag();
			if (holder != null) {
				if (holder.pathType != DataHolder.PATH_NET) {
					File mediaFile = holder.mediaFile;
					if (!activity.isFinishing()) {
						mProgressDialog = PublicFunctions.showProgressDialog(
								mContext, -1,
								R.string.please_wait_for_a_sec, true, false,
								mProgressDialog);
						PublicFunctions.doOpenFile(mediaFile.getPath(),
								mContext, mProgressDialog);
					}
				} else if (holder.pathType == DataHolder.PATH_NET) {
					if (!activity.isFinishing())
						mProgressDialog = PublicFunctions.showProgressDialog(
								mContext, -1,
								R.string.download_in_progress, true, false,
								mProgressDialog);

					final DownloadFileByUUID dl = new DownloadFileByUUID(mContext);
					DowloadListener l = new DowloadListener() {

						@Override
						public void onDownloadComplete() {
							if (!activity.isFinishing())
								mProgressDialog.dismiss();
							String downloadPath = dl.getDownloadPath();
							if (!PublicFunctions.isStringNullOrEmpty(downloadPath)) {
								holder.mediaFile = new File(downloadPath);
								holder.pathType = DataHolder.PATH_SAVED;
								mDetailItem.setMediaDataHolders(mMediaDataHolders);
								if (mMediaDownloadListener != null) {
									mMediaDownloadListener.onMediaDownload(mMediaDataHolders);
								}
							}
						}

					};

					dl.setListener(l);
					dl.mShouldLockFile = false;
					dl.execute(holder.fileId);
				}
			}
		}
	};*/

	public void setOnItemSelectListener(OnItemSelectListener l) {
		this.mOnItemSelectListener = l;
	}

	public void setOnSelectListener(OnSelectListener l) {
		this.mOnSelectListener = l;
	}

	public CommonItemView(Context context) {
		super(context);
		mContext = context;
		initView(context);
	}

	public CommonItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setCustomAttributes(attrs);
		initView(context);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public CommonItemView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		setCustomAttributes(attrs);
		initView(context);
	}

	public void setDetailItem(CommonItem detailItem) {
		this.mDetailItem = detailItem;
		initItemView();
		ItemStyle itemStyle = detailItem.getItemStyle();
		setTitle(detailItem.getTitle());
		if (detailItem.getItemStyle() != null) {
			setItemStyle(detailItem.getItemStyle());
		}
		if (!StringUtil.isStringNullOrEmpty(detailItem.getValue())) {
			if (itemStyle == ItemStyle.EDIT) {
				setEditValue(detailItem.getValue());
			} else {
				setTextValue(detailItem.getValue());
			}
		}
		if (detailItem.getDrawableId() != -1) {
			setButtonView(detailItem.getDrawableId(), detailItem.getImageClickListener());
		}
		setViewClickListener(detailItem.getViewClickListener());
		if (detailItem.isArrowViewVisible()) {
			setIsArrowViewVisible(true);
		}
		/*if (detailItem.getMediaDataHolders() != null) {
			setMediaDataHolders(detailItem.getMediaDataHolders());
		}*/
		if (!StringUtil.isStringNullOrEmpty(detailItem.getHint())) {
			mValueText.setHint(detailItem.getHint());
		}
	}

	/*public void setOnMediaDownloadListener(MediaDownloadListener l) {
		this.mMediaDownloadListener = l;
	}*/

	public void initItemView() {
		setTitleAndValue("", "");
		setViewClickListener(null);
		setItemStyle(ItemStyle.DEFAULT);
	}

	public void setItemStyle(ItemStyle style) {
		this.mStyle = style;
		LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
		if (mStyle == ItemStyle.SELECT) {
			setIsArrowViewVisible(true);
			mValueText.setVisibility(View.VISIBLE);
			mValueText.setSingleLine(false);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = mContext.getResources().getDimensionPixelOffset(R.dimen.ebei_item_common_height);
		} else if (mStyle == ItemStyle.EDIT) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.VISIBLE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = mContext.getResources().getDimensionPixelOffset(R.dimen.ebei_item_common_height);

			mValueEdit.setBackgroundResource(R.color.ebei_transparent);
			mValueEdit.setClickable(true);
			mValueEdit.setEnabled(true);
			mValueEdit.setFocusable(true);
			mValueEdit.setFocusableInTouchMode(true);
			mValueEdit.setOnClickListener(null);
		} else if (mStyle == ItemStyle.BOOL) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setVisibility(View.VISIBLE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = mContext.getResources().getDimensionPixelOffset(R.dimen.ebei_item_common_height);

			mImageView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int imageId = 0;
					if (v.getTag() != null) {
						imageId = StringUtil.parseInt(v.getTag().toString());
					}
					if (imageId == R.drawable.ebei_switch_close) {
						if (mOnSelectListener != null) {
							mImageView.setImageResource(R.drawable.ebei_switch_open);
							mImageView.setTag(R.drawable.ebei_switch_open);
							mOnSelectListener.onSelect(true);
						}
					} else {
						if (mOnSelectListener != null) {
							mImageView.setImageResource(R.drawable.ebei_switch_close);
							mImageView.setTag(R.drawable.ebei_switch_close);
							mOnSelectListener.onSelect(false);
						}
					}
				}
			});
			mImageView.setImageResource(R.drawable.ebei_switch_close);
			mImageView.setTag(R.drawable.ebei_switch_close);
		} else if (mStyle == ItemStyle.BUTTON) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.VISIBLE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = LayoutParams.WRAP_CONTENT;
		} else if (mStyle == ItemStyle.SELECT_SINGLE) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.VISIBLE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = LayoutParams.WRAP_CONTENT;

//			mSelectSingleItemViews = new ArrayList<RadioButton>();
//			mSelectSingleItemViews.add((RadioButton) findViewById(R.id.rbtnRequestSatisfied));
//			mSelectSingleItemViews.add((RadioButton) findViewById(R.id.rbtnCorrective));
		} else if (mStyle == ItemStyle.NUMBER) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.VISIBLE);
//			mSingleSelectCheckBoxs.setVisibility(View.GONE);
			lp.height = mContext.getResources().getDimensionPixelOffset(R.dimen.ebei_item_common_height);
		} else if (mStyle == ItemStyle.ITEMS_GRID) {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.GONE);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
//			mSingleSelectCheckBoxs.setVisibility(View.VISIBLE);
			lp.height = LayoutParams.WRAP_CONTENT;
		} else {
			setIsArrowViewVisible(false);
			mValueText.setVisibility(View.VISIBLE);
			mValueText.setSingleLine(false);
			mValueEdit.setVisibility(View.GONE);
			mImageView.setOnClickListener(null);
			mImageView.setVisibility(View.GONE);
			mBtnRight.setVisibility(View.GONE);
			mBtnTextRight.setVisibility(View.GONE);
			mMediaParentView.setVisibility(View.GONE);
//			mSelectSingleView.setVisibility(View.GONE);
			mNumberView.setVisibility(View.GONE);
			lp.height = LayoutParams.WRAP_CONTENT;
		}
		mContentView.setLayoutParams(lp);

		if (mValueEdit.getVisibility() == View.VISIBLE) {
			mValueEdit.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					// TODO Auto-generated method stub
					if (mTextWatcher != null) {
						mTextWatcher.onTextChanged(s, start, before, count);
					}
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
											  int after) {
					// TODO Auto-generated method stub
					if (mTextWatcher != null) {
						mTextWatcher.beforeTextChanged(s, start, count, after);
					}
				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					if (mDetailItem != null) {
						mDetailItem.setValue(s.toString());
					}
					if (mTextWatcher != null) {
						mTextWatcher.afterTextChanged(s);
					}
				}
			});
		}
	}

	public void setIsHeaderViewVisible(boolean isVisible) {
		setIsHeaderViewVisible(isVisible, false);
	}

	public void setIsHeaderViewVisible(boolean isVisible, boolean isFirst) {
		mHeaderView.setVisibility(isVisible ? View.VISIBLE : View.GONE);
		int margin = mItemMargin;
		if (isFirst) {
			margin = 0;
		}
		mHeaderView.setPadding(0, margin, 0, 0);
	}

	public void setIsFooterViewVisible(boolean isVisible) {
		setIsFooterViewVisible(isVisible, false);
	}

	public void setIsFooterViewVisible(boolean isVisible, boolean isLast) {
		mFooterView.setVisibility(isVisible ? View.VISIBLE : View.GONE);
		int margin = mItemMargin;
		if (!isLast) {
			margin = 0;
		}
		mFooterView.setPadding(0, 0, 0, margin);
		if (isVisible) {
			setIsDividerViewVisible(false);
		}
	}

	public void setIsDividerViewVisible(boolean isVisible) {
		mDividerView.setVisibility(isVisible ? View.VISIBLE : View.GONE);
		if (isVisible) {
			setIsFooterViewVisible(false);
		}
	}

	public void setTitle(String title) {
		mTitleText.setText(title);
	}

	public void setTitle(int titleId) {
		mTitleText.setText(titleId);
	}

	public void setTitleColor(int color) {
		mTitleText.setTextColor(color);
	}

	public void setTitleMinWidth(int width) {
		mTitleText.setMinWidth(width);
	}

	public void setTextValue(int valueId) {
		mValueText.setText(valueId);
	}

	public void setTextValue(String value) {
		mValueText.setText(StringUtil.getDefaultString(value));
	}

	public void setTextColor(int color) {
		mValueText.setTextColor(color);
	}

	public String getTextValue() {
		return mValueText.getText().toString();
	}

	public void setEditValue(int valueId) {
		mValueEdit.setText(valueId);
	}

	public void setEditValue(String value) {
		mValueEdit.setText(StringUtil.getDefaultString(value));
	}

	public void setEditHint(String hint) {
		mValueEdit.setHint(StringUtil.getDefaultString(hint));
	}

	public void setEditHint(int hintId) {
		mValueEdit.setHint(hintId);
	}

	public void setEditBackgroundResource(int resid) {
		mValueEdit.setBackgroundResource(resid);
	}

	public String getEditValue() {
		return mValueEdit.getText().toString();
	}

	public void setEditColor(int color) {
		mValueEdit.setTextColor(color);
	}

	public void setTextSingleLine(boolean singleLine) {
		mValueText.setSingleLine(singleLine);
	}

	public void setTitleAndValue(String title, String value) {
		setTitle(title);
		setTextValue(value);
	}

	public void setEditForTelephoneType() {
		mValueEdit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
	}

	public void setEditForNumberDecimal() {
		mValueEdit.setInputType(8194);
	}

	public void setEditForPasswordType() {
		mValueEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
	}

	public void setEditInput(int type) {
		mValueEdit.setInputType(type);
	}

	public void addTextChangedListener(TextWatcher textWatcher) {
		this.mTextWatcher = textWatcher;
	}

	public EditText getValueEditText() {
		return mValueEdit;
	}

	public void setViewClickListener(OnClickListener l) {
		setOnClickListener(l);
	}

	public void setIsArrowViewVisible(boolean isVisible) {
		if (isVisible) {
			mArrowView.setVisibility(View.VISIBLE);
		} else {
			mArrowView.setVisibility(View.GONE);
		}
	}

	public void setViewBackgroundResource(int resid) {
		// TODO Auto-generated method stub
		mContentView.setBackgroundResource(resid);
	}

	public void setOnNumberVariateListener(NumberVariateView.OnNumberVariateListener l) {
		mNumberView.setOnNumberVariateListener(l);
	}

	public void setBorder(int border) {
		mNumberView.setBorder(border);
	}

	public void setNumber(float number) {
		mNumberView.setNumber(number);
	}

	public float getNumber() {
		return mNumberView.getNumber();
	}

	/*public void setMediaDataHolders(List<DataHolder> mediaDataHolders) {
		this.mMediaDataHolders = mediaDataHolders;
		mMediaLinearLayout.removeAllViews();
		for (int i = 0; i < mediaDataHolders.size(); i++) {
			DataHolder holder = mediaDataHolders.get(i);

			int layoutId = getLayoutId(holder);

			if (layoutId != -1) {
				View v = mInflater.inflate(layoutId, null);
				v.setOnClickListener(listener);
				v.setClickable(true);
				v.setTag(holder);
				mMediaLinearLayout.addView(v);
			}
		}

		mMediaParentView.setVisibility(View.VISIBLE);
	}

	private int getLayoutId(DataHolder holder) {
		int layoutId = -1;
		int attachType = holder.attachType;
		int type = holder.type;
		String doModified = holder.doModified;

		if (attachType == 1) {
			type = DataHolder.IMAGE_TYPE;
		} else if (attachType == 2) {
			type = DataHolder.VEDIO_TYPE;
		} else if (attachType == 3 || attachType == 4) {
			type = DataHolder.AUDIO_TYPE;
		}

		if (mTitleText != null && mContext.getString(R.string.sign).equals(mTitleText.getText().toString())) {
			if (holder.pathType != DataHolder.PATH_NET) {
				layoutId = R.layout.sign_attach_button;
			} else {
				layoutId = R.layout.sign_attach_button;
			}
		} else {
			if (type == DataHolder.IMAGE_TYPE) {
				if (holder.pathType != DataHolder.PATH_NET) {
					if (QpiAttachment.ATTACHMENT_DO_MODIFIED.equals(doModified)) {
						layoutId = R.layout.image_attach_modified_button_downloaded;
					} else {
						layoutId = R.layout.image_attach_button;
					}
				} else {
					if (QpiAttachment.ATTACHMENT_DO_MODIFIED.equals(doModified)) {
						layoutId = R.layout.image_attach_modified_button_undownload;
					} else {
						layoutId = R.layout.image_attach_button_n;
					}
				}
			} else if (type == DataHolder.VEDIO_TYPE) {
				if (holder.pathType != DataHolder.PATH_NET) {
					layoutId = R.layout.video_attach_button;
				} else {
					layoutId = R.layout.video_attach_button_n;
				}
			} else if (type == DataHolder.AUDIO_TYPE) {
				if (holder.pathType != DataHolder.PATH_NET) {
					layoutId = R.layout.audio_attach_button;
				} else {
					layoutId = R.layout.audio_attach_button_n;
				}
			}
		}
		return layoutId;
	}*/

	public void setButtonView(int drawableId, OnClickListener l) {
		mImageView.setOnClickListener(l);
		if (l == null) {
			mImageView.setVisibility(View.GONE);
		} else {
			mImageView.setImageResource(drawableId);
			mImageView.setVisibility(View.VISIBLE);
		}
	}

	public void setRightButtonView(String buttonText, OnClickListener l) {
		mBtnRight.setOnClickListener(l);
		if (l == null) {
			mBtnRight.setVisibility(View.GONE);
		} else {
			mBtnRight.setText(buttonText);
			mBtnRight.setVisibility(View.VISIBLE);
		}
	}

	public void setRightButtonTextView(String text, OnClickListener l) {
		mBtnTextRight.setOnClickListener(l);
		if (StringUtil.isEmpty(text)) {
			mBtnTextRight.setVisibility(View.GONE);
		} else {
			mBtnTextRight.setText(text);
			mBtnTextRight.setVisibility(View.VISIBLE);
		}
	}

	public void setIsDeadlineCanSelect(boolean isDeadlineCanSelect) {
		this.mIsDeadlineCanSelect = isDeadlineCanSelect;
	}

	public int getSelectSingleSelectPosition() {
		for (int i = 0; i < mSelectSingleItemViews.size(); i++) {
			if (mSelectSingleItemViews.get(i).isChecked()) {
				return i;
			}
		}
		return 0;
	}

	public List<RadioButton> getSelectSingleItemViews() {
		return mSelectSingleItemViews;
	}

	/*public void setItemsGrid(final List<String> items) {
		final Map<Integer, String> mSingleSelect = new HashMap<Integer, String>();
		for (int i = 0; i < items.size(); i++) {
			mSingleSelect.put(i, items.get(i));
		}
		mSingleSelectCheckBoxs.setType(SingleSelectType.TEXT);
		mSingleSelectCheckBoxs.setData(mSingleSelect);
		mSingleSelectCheckBoxs.setOnSelectListener(new SingleSelectCheckBoxs.OnSelectListener() {

			@Override
			public void onSelect(int position, boolean isChecked) {
				// TODO Auto-generated method stub
				if (mOnItemSelectListener != null) {
					mOnItemSelectListener.onItemSelect(items.get(position));
				}
			}

		});
	}

	public void setItemsGridSelect(String itemName) {
		mSingleSelectCheckBoxs.setItemSelect(itemName);
	}

	@Override
	public void setVisibility(int visibility) {
		// TODO Auto-generated method stub
		super.setVisibility(visibility);
		mSingleSelectCheckBoxs.setVisibility(visibility);
	}*/

	private void setCustomAttributes(AttributeSet attrs) {
		TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.EbeiCommonItemView);
		mTitle = a.getString(R.styleable.EbeiCommonItemView_item_title);
		String style = a.getString(R.styleable.EbeiCommonItemView_item_style);
		if (mContext.getString(R.string.ebei_common_item_style_select).equals(style)) {
			mStyle = ItemStyle.SELECT;
		} else if (mContext.getString(R.string.ebei_common_item_style_edit).equals(style)) {
			mStyle = ItemStyle.EDIT;
		} else if (mContext.getString(R.string.ebei_common_item_style_select_single).equals(style)) {
			mStyle = ItemStyle.SELECT_SINGLE;
		} else if (mContext.getString(R.string.ebei_common_item_style_number).equals(style)) {
			mStyle = ItemStyle.NUMBER;
		}
		isDividerVisible = a.getBoolean(R.styleable.EbeiCommonItemView_item_dividerVisible, true);
		isHearderVisible = a.getBoolean(R.styleable.EbeiCommonItemView_item_headerVisible, true);
		isFooterVisible = a.getBoolean(R.styleable.EbeiCommonItemView_item_footerVisible, true);
	}

	private void initView(Context context) {
		mItemMargin = ViewUtil.dp2px(mContext, 20);
		mInflater = LayoutInflater.from(mContext);
		mRootView = mInflater.inflate(R.layout.ebei_view_common_item, null);
		mHeaderView = mRootView.findViewById(R.id.view_header);
		mFooterView = mRootView.findViewById(R.id.view_footer);
		mTitleText = mRootView.findViewById(R.id.tv_title);
		mValueText = mRootView.findViewById(R.id.tv_value);
		mValueEdit = mRootView.findViewById(R.id.et_value);
		mMediaLinearLayout = mRootView.findViewById(R.id.attachment_layout);
		mMediaParentView = mRootView.findViewById(R.id.HorizontalScrollView);
		mNumberView = mRootView.findViewById(R.id.view_number);
		mArrowView = mRootView.findViewById(R.id.iv_arrow);
		mImageView = mRootView.findViewById(R.id.iv_image);
		mBtnRight = mRootView.findViewById(R.id.btn_right);
		mBtnTextRight = mRootView.findViewById(R.id.tv_button_text);
		viewRight = mRootView.findViewById(R.id.view_right);
		mContentView = mRootView.findViewById(R.id.view_content);
		mDividerView = mRootView.findViewById(R.id.view_divider);
		addView(mRootView);

		int width = ViewUtil.getScreenSize((Activity) mContext).widthPixels * 1 / 4;
		mTitleText.setMinWidth(width);
		mTitleText.setMaxWidth(width);

		mTitleText.setText(mTitle);
		setItemStyle(mStyle);
		if (!isDividerVisible) {
			setIsDividerViewVisible(false);
		}
		setIsHeaderViewVisible(isHearderVisible, true);
		setIsFooterViewVisible(isFooterVisible, false);
	}

}
